package com.hex.liyue.usermodule.presenter;

import android.text.TextUtils;

import com.hex.liyue.commonmodule.bean.BaseBean;
import com.hex.liyue.commonmodule.manager.HttpManager;
import com.hex.liyue.commonmodule.manager.UserManager;
import com.hex.liyue.commonmodule.net.ResponseListener;
import com.hex.liyue.commonmodule.utils.RegularUtils;

/**
 * Created by WJ on 2016/6/21.
 */
public class EditAddressPresenter {
    private IEditAddressView iEditAddressView;

    public EditAddressPresenter(IEditAddressView iEditAddressView) {
        this.iEditAddressView = iEditAddressView;
    }

    public void submitAddress() {
        if (TextUtils.isEmpty(iEditAddressView.getUsername())) {
            iEditAddressView.showT("请输入收货人");
            return;
        }
        if (!RegularUtils.phone(iEditAddressView.getMobile())) {
            iEditAddressView.showT("请输入正确的联系方式");
            return;
        }
        if (TextUtils.isEmpty(iEditAddressView.getProvinceId())) {
            iEditAddressView.showT("请选择所在地区");
            return;
        }
        if (TextUtils.isEmpty(iEditAddressView.getAddressDetails())) {
            iEditAddressView.showT("请输入详细的地址");
            return;
        }
        if(iEditAddressView.getType()==1) {
            //这是编辑地址
            iEditAddressView.showLod("加载中...");
            HttpManager.getInstance().upadteAddress(
                    iEditAddressView.getDeliveryId(),
                    UserManager.getUserId(),
                    iEditAddressView.getUsername(),
                    iEditAddressView.getMobile(),
                    iEditAddressView.getAddressDetails(),
                    iEditAddressView.getProvinceId(),
                    iEditAddressView.getProvinceName(),
                    iEditAddressView.getCityId(),
                    iEditAddressView.getCityName(),
                    iEditAddressView.getDistrictId(),
                    iEditAddressView.getDistrictName(),
                    new ResponseListener<BaseBean>() {
                @Override
                public void onSuccess(BaseBean baseBean) {
                    iEditAddressView.showT("编辑收货地址成功");
                    iEditAddressView.closeLod();
                    iEditAddressView.closeCurrentActivity();
                }

                @Override
                public void onFailure(String responseresult) {
                    iEditAddressView.showT(responseresult);
                    iEditAddressView.closeLod();
                }
            });
        }else if (iEditAddressView.getType() == 2) {
            //这是添加地址
            iEditAddressView.showLod("加载中...");
            HttpManager.getInstance().addAddress(
                    UserManager.getUserId(),
                    iEditAddressView.getUsername(),
                    iEditAddressView.getMobile(),
                    iEditAddressView.getAddressDetails(),
                    iEditAddressView.getProvinceId(),
                    iEditAddressView.getProvinceName(),
                    iEditAddressView.getCityId(),
                    iEditAddressView.getCityName(),
                    iEditAddressView.getDistrictId(),
                    iEditAddressView.getDistrictName(),
                    new ResponseListener<BaseBean>() {
                @Override
                public void onSuccess(BaseBean baseBean) {
                    iEditAddressView.showT("新增收货地址成功");
                    iEditAddressView.closeLod();
                    iEditAddressView.closeCurrentActivity();
                }

                @Override
                public void onFailure(String responseresult) {
                    iEditAddressView.showT(responseresult);
                    iEditAddressView.closeLod();
                }
            });
        }

    }
}
